const express = require('express');
const router = express.Router();
const {query} = require('../models/db');


router.get('/:sort', async (req, res, next) => {
    try {
        const dress = await query('SELECT * FROM sure.goods where sort=\'dress\'');
        const agent = await query('SELECT * FROM sure.goods where sort=\'agent\'');
        const coat = await query('SELECT * FROM sure.goods where sort=\'coat\'');
        const jacket = await query('SELECT * FROM sure.goods where sort=\'jacket\'');
        const trousers = await query('SELECT * FROM sure.goods where sort=\'trousers\'');
        const child = await query('SELECT * FROM sure.goods where sort=\'child\'');


        res.render('page/goods/' + req.params.sort,
            {
                title: getTitle(res, req.params.sort) + ' - SURE后台管理系统',
                dress, agent, coat, jacket, trousers, child
            }
        );
    } catch (err) {
        next(err);
    }
});

function getTitle(res, sort) {
    for (const c of res.locals.uname) {
        if (c.sort.indexOf(sort) !== -1) {
            return c.type;
        }
    }
    return ''
}

module.exports = router;